package com.yunmaozj.tools.site.server.util;

import com.yunmaozj.tools.site.server.dto.Developer;
import lombok.AllArgsConstructor;
import lombok.NonNull;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.dom.DOMElement;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.SAXWriter;
import org.dom4j.io.XMLWriter;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;
import org.xml.sax.SAXException;

import javax.sql.rowset.spi.XmlWriter;
import java.io.*;
import java.util.List;

import static org.apache.commons.lang3.StringUtils.isBlank;

/**
 * 文件管理
 *
 * @author zhongminghong
 * @email zhongmh@yunmaozj.com
 * @create 2022-05-26 23:34
 */
public class FileManager {

    private final String rootDir;

    public FileManager(String rootDir) {
        File file = new File(rootDir);
        if (!file.exists() || !file.isDirectory()) {
            file.mkdirs();
        }
        this.rootDir = rootDir;

    }

    /**
     * 上传文件
     *
     * @param projectId 项目名称
     * @param file
     * @return
     */
    public boolean uploadFile(@NonNull String projectId, String path, MultipartFile file) throws IOException {
        File projectDirectory = new File(rootDir, projectId);
        //创建项目目录
        if (!projectDirectory.exists() || !projectDirectory.isDirectory()) {
            projectDirectory.mkdirs();
        }
        OutputStream os;
        if (isBlank(path)) {
            os = new FileOutputStream(new File(projectDirectory, file.getOriginalFilename()));
        } else {
            File outFile = new File(projectDirectory, path);
            if (!outFile.exists() || !outFile.isDirectory()) {
                outFile.mkdirs();
            }
            os = new FileOutputStream(new File(projectDirectory, path + "/" + file.getOriginalFilename()));
        }
        try {
            IOUtils.copy(file.getInputStream(), os);
        } finally {
            IOUtils.closeQuietly(os);
        }
        return true;
    }

    public boolean existsMetadataFile(@NonNull String projectId) {
        File projectDirectory = new File(rootDir, projectId);
        if (projectDirectory.exists() && projectDirectory.isDirectory()) {
            File projectMetadataFile = new File(projectDirectory, "project-metadata.xml");
            return projectMetadataFile.exists();
        }
        return false;
    }

    public boolean uploadProjectMetadataFile(@NonNull String projectId, List<Developer> developers) throws IOException, DocumentException {
        return false;
    }

    public File getProjectDirectory(@NonNull String projectId) {
        File projectDirectory = new File(rootDir, projectId);
        //创建项目目录
        if (!projectDirectory.exists() || !projectDirectory.isDirectory()) {
            projectDirectory.mkdirs();
        }
        return projectDirectory;
    }

    public File getRootDirectory() {
        return new File(rootDir);
    }
}
